let express = require('express');
let router = express.Router();
let fs = require("fs");
let validate = require("../util/validateUtil");
// npmmysql implmentation for test
let userDao = require("../dao/mysql/npm_mysql/userDao");
// sequelize implmentation for test
let userDaoSeq = require("../dao/mysql/npm_sequelize/userDaoSeq");
let userEntity = require("../dao/mysql/npm_sequelize/entity/userEntity");


/**************************************************************************************************
 * get a user by id , like RESTFUL API
 **************************************************************************************************/
// router.get('/:id',function(req, resp , next) {
//   // 接收形如/users/1这样格式的参数
//   let id = req.params.id;
//   // 在任何模块文件内部，可以使用__dirname变量获取当前模块文件所在目录的完整绝对路径
//   let path = __dirname + "/users.json";
//   fs.readFile(path , "utf-8", function (err, data) {
//     if(data == null || data == undefined) {
//       data = "can not get users";
//     }
//     resp.end(data);
//   })
// });

/**************************************************************************************************
 * get all users
 **************************************************************************************************/
router.get("/",function(req , resp , next){
  userDao.selectAllUsers(function(rs){
    console.log(rs);
    // console.log(JSON.stringify(rs));
    if(rs == null || rs == undefined) {
      resp.end("rs null or undefined");
    } else {
      if(rs.length > 20){
        resp.end("so many users, so hidden");
      }
      resp.send(JSON.stringify(rs));
    }
  });
});

/**************************************************************************************************
 * user login action
 **************************************************************************************************/
router.post("/doLogin",function (req , resp , next) {
  let username = req.body.username;
  let password = req.body.password;

  // validate params
  if(validate.isEmpty(username) && validate.isEmpty(password)) {
    resp.send("param error");
    return;
  }

  userDao.login(username, password, function (rs) {
    console.log(rs);
    if(validate.isEmpty(rs)) {
      resp.send("username or password incorrect");
    } else {
      resp.redirect(301,"/angular");
    }
  });
});


/**************************************************************************************************
 * user register action
 **************************************************************************************************/
router.post("/doRegister",function (req,resp) {
  let userName = req.body.userName;
  let userPassword = req.body.userPassword;

  // validate params
  if(validate.isEmpty(userName) && validate.isEmpty(userPassword)) {
    resp.send("param error");
    return;
  }
  userEntity.userName = userName;
  userEntity.userPassword = userPassword;
  try {
    userDaoSeq.insert(userEntity);
  } catch (e) {
    console.log(e)
  }
  resp.redirect(301,"/");
});


router.get("/get",function (req, resp, next) {
    let userId = req.body.userId;
    userId = 1;
    userEntity.userId = userId;
    // try {
    //     let user = userDao.selectById(userEntity);
    //     console.log(user);
    // } catch(e) {
    //   console.log(e);
    // }

    userEntity.findById(userEntity.userId).then(
        (data)=> {
            resp.send(data);
            console.log(data);
            return data;
        }
    ).catch(
        (err)=> console.log(err)
    );
});

module.exports = router;
